package com.fc.v2.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 利用HttpClient进行post请求的工具类
 * @ClassName: HttpClientUtil 
 * @Description: TODO
 * @author Devin <xxx> 
 * @date 2017年2月7日 下午1:43:38 
 *  
 */
public class HttpClientUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public static String doPost(String url,Map<String,Object> map){
        logger.info("请求的地址：{}" , url);
        logger.info("请求的参数：{}" , map);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int times = 0;
        while (times < 10) {
            HttpClient httpClient;
            HttpPost httpPost;
            String result;
            try{
                httpClient = new SSLClient();
                httpPost = new HttpPost(url);
                httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");
                StringEntity s = new StringEntity(JSONObject.toJSONString(map),"utf-8");
                httpPost.setEntity(s);
                HttpResponse response = httpClient.execute(httpPost);
                if(response != null){
                    HttpEntity resEntity = response.getEntity();
                    if(resEntity != null){
                        result = EntityUtils.toString(resEntity,"utf-8");
                        logger.info("返回结果：{}", result);
                        return result;
                    }
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }
            times = times + 1;
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String page = "2";
        String limit = "10";

        JSONObject a = new JSONObject();
        a.put("cert_id", "5003400");
        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", limit);
        params.put("jsonStr", a);
        String url  = "https://yc.nxzhzj.cn/estateTradeR01/portals/query!statiPerSalePermitData?certno=" + URLEncoder.encode("（银审服）预售证第 20210495号", "UTF-8");
        //String url  = "https://yc.nxzhzj.cn/estateTradeR01/portals/query!listSalePermitProject";
        //String url  = "https://yc.nxzhzj.cn/estateTradeR01/portals/query!listSalePermitBuild?jsonStr=" + URLEncoder.encode(a.toJSONString(), "UTF-8");
        doPost(url, params);
    }
}